﻿Shader "Unlit/Blur"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        _BlurScale("Blur Scale", float) = 0.1
        _ColorTex("Color Texture",2D) = "black"{}
        _BlurTex("Blur Texture",2D) = "black"{}
        _AlphaClip("Alpha Clip", Range(0,1)) = 0.1
    }

    CGINCLUDE
        #include "UnityCG.cginc"

        struct appdata
        {
            float4 vertex : POSITION;
            float2 uv : TEXCOORD0;
        };

        struct v2f
        {
            float4 vertex : SV_POSITION;
            float2 uv[5] : TEXCOORD0;
        };

        struct v2f2
        {
            float4 vertex : SV_POSITION;
            float2 uv : TEXCOORD0;
        };

        sampler2D _MainTex;
        float4 _MainTex_TexelSize;
        sampler2D _ColorTex;
        float4 _ColorTex_TexelSize;
        sampler2D _BlurTex;
        float4 _BlurTex_TexelSize;
        float _BlurScale;
        float _AlphaClip;

        v2f2 vert (appdata v)
        {
            v2f2 o;
            o.vertex = UnityObjectToClipPos(v.vertex);
            o.uv = v.uv;
            return o;
        }

        v2f vert_hor(appdata v){
            v2f o;
            o.vertex = UnityObjectToClipPos(v.vertex);
            float2 uv=v.uv;
            o.uv[0] = uv;
            o.uv[1] = uv + float2(1,0) * _BlurScale * _MainTex_TexelSize.xy;
            o.uv[2] = uv + float2(-1,0) * _BlurScale * _MainTex_TexelSize.xy;
            o.uv[3] = uv + float2(2,0) * _BlurScale * _MainTex_TexelSize.xy;
            o.uv[4] = uv + float2(-2,0) * _BlurScale * _MainTex_TexelSize.xy;
            return o;
        }

        v2f vert_ver(appdata v){
            v2f o;
            o.vertex = UnityObjectToClipPos(v.vertex);
            float2 uv=v.uv;
            o.uv[0] = uv;
            o.uv[1] = uv + float2(0,1) * _BlurScale * _MainTex_TexelSize.xy;
            o.uv[2] = uv + float2(0,-1) * _BlurScale * _MainTex_TexelSize.xy;
            o.uv[3] = uv + float2(0,2) * _BlurScale * _MainTex_TexelSize.xy;
            o.uv[4] = uv + float2(0,-2) * _BlurScale * _MainTex_TexelSize.xy;
            return o;
        }

        fixed4 frag(v2f i) : SV_TARGET
        {
            float3 col = tex2D(_MainTex,i.uv[0]).xyz *0.4026;
            float3 col1 = tex2D(_MainTex,i.uv[1]).xyz *0.2442;
            float3 col2 = tex2D(_MainTex,i.uv[2]).xyz *0.2442;
            float3 col3 = tex2D(_MainTex,i.uv[3]).xyz *0.0545;
            float3 col4 = tex2D(_MainTex,i.uv[4]).xyz *0.0545;
            float3 finalCol = col+col1+col2+col3+col4;
            return fixed4(finalCol,1.0);
        }

        fixed4 cutFrag (v2f2 i) : SV_Target
        {
            fixed4 color = tex2D(_MainTex, i.uv);
            fixed4 commandColor = tex2D(_ColorTex, i.uv);
            fixed4 edge = color-commandColor;
            fixed flag = step(_AlphaClip, edge.r);
            return edge*flag;
        }

        fixed4 combineFrag (v2f2 i) : SV_Target
        {
            fixed4 color = tex2D(_MainTex, i.uv);
            fixed4 commandColor = tex2D(_BlurTex, i.uv);
            fixed4 finalColor = color+commandColor;
            return finalColor;
        }
    ENDCG

    SubShader
    {
        // No culling or depth
        Cull Off ZWrite Off ZTest Always

        // 0: blur hor
        Pass
        {
            CGPROGRAM
            #include "UnityCG.cginc"
                #pragma vertex vert_hor
                #pragma fragment frag
            ENDCG
        }

        // 1: blur ver
        Pass
        {
            CGPROGRAM
            #include "UnityCG.cginc"
                #pragma vertex vert_ver
                #pragma fragment frag
            ENDCG
        } 

        // 2: cut
        Pass
        {
            CGPROGRAM
            #include "UnityCG.cginc"
                #pragma vertex vert
                #pragma fragment cutFrag
            ENDCG
        }

        // 3: combine
         Pass
        {
            CGPROGRAM
            #include "UnityCG.cginc"
                #pragma vertex vert
                #pragma fragment combineFrag
            ENDCG
        }
    }
}